home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / previe_1 / sample.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-05-07  |  10.0 KB  |  299 lines

  1. VERSION 5.00
  2. Begin VB.Form frmSample 
  3.    Caption         =   "Sample"
  4.    ClientHeight    =   3750
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   4425
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   3750
  10.    ScaleWidth      =   4425
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CommandButton btnEndDoc 
  13.       Caption         =   "End Document"
  14.       Enabled         =   0   'False
  15.       Height          =   375
  16.       Left            =   120
  17.       TabIndex        =   11
  18.       Top             =   1560
  19.       Width           =   1695
  20.    End
  21.    Begin VB.Timer Timer 
  22.       Enabled         =   0   'False
  23.       Interval        =   500
  24.       Left            =   3960
  25.       Top             =   2520
  26.    End
  27.    Begin VB.CommandButton btnSetup 
  28.       Caption         =   "Printer Setup"
  29.       Height          =   375
  30.       Left            =   120
  31.       TabIndex        =   9
  32.       Top             =   2640
  33.       Width           =   1695
  34.    End
  35.    Begin VB.CommandButton btnClose 
  36.       Cancel          =   -1  'True
  37.       Caption         =   "Close"
  38.       Height          =   375
  39.       Left            =   120
  40.       TabIndex        =   4
  41.       Top             =   3240
  42.       Width           =   1695
  43.    End
  44.    Begin VB.CommandButton btnPreview 
  45.       Caption         =   "Preview Document"
  46.       Enabled         =   0   'False
  47.       Height          =   375
  48.       Left            =   120
  49.       TabIndex        =   3
  50.       Top             =   2040
  51.       Width           =   1695
  52.    End
  53.    Begin VB.CommandButton btnNewPage 
  54.       Caption         =   "New Page"
  55.       Enabled         =   0   'False
  56.       Height          =   375
  57.       Left            =   120
  58.       TabIndex        =   2
  59.       Top             =   1080
  60.       Width           =   1695
  61.    End
  62.    Begin VB.CommandButton btnDraw 
  63.       Caption         =   "Draw Page Items"
  64.       Enabled         =   0   'False
  65.       Height          =   375
  66.       Left            =   120
  67.       TabIndex        =   1
  68.       Top             =   600
  69.       Width           =   1695
  70.    End
  71.    Begin VB.CommandButton btnNewDoc 
  72.       Caption         =   "New Document"
  73.       Height          =   375
  74.       Left            =   120
  75.       TabIndex        =   0
  76.       Top             =   120
  77.       Width           =   1695
  78.    End
  79.    Begin VB.Label Label6 
  80.       AutoSize        =   -1  'True
  81.       Caption         =   "Step 3: end the document"
  82.       Height          =   195
  83.       Left            =   2040
  84.       TabIndex        =   12
  85.       Top             =   1650
  86.       Width           =   1845
  87.    End
  88.    Begin VB.Label Label5 
  89.       Caption         =   "At any time, printer setup dialog may be shown"
  90.       Height          =   435
  91.       Left            =   2040
  92.       TabIndex        =   10
  93.       Top             =   2610
  94.       Width           =   2175
  95.    End
  96.    Begin VB.Label Label4 
  97.       AutoSize        =   -1  'True
  98.       Caption         =   "Step 4: preview && print"
  99.       Height          =   195
  100.       Left            =   2040
  101.       TabIndex        =   8
  102.       Top             =   2130
  103.       Width           =   1590
  104.    End
  105.    Begin VB.Label Label3 
  106.       AutoSize        =   -1  'True
  107.       Caption         =   "Step 2b: start a new page"
  108.       Height          =   195
  109.       Left            =   2040
  110.       TabIndex        =   7
  111.       Top             =   1170
  112.       Width           =   1830
  113.    End
  114.    Begin VB.Label Label2 
  115.       AutoSize        =   -1  'True
  116.       Caption         =   "Step 2a: draw items on a page"
  117.       Height          =   195
  118.       Left            =   2040
  119.       TabIndex        =   6
  120.       Top             =   690
  121.       Width           =   2160
  122.    End
  123.    Begin VB.Label Label1 
  124.       AutoSize        =   -1  'True
  125.       Caption         =   "Step 1: create a new document"
  126.       Height          =   195
  127.       Left            =   2040
  128.       TabIndex        =   5
  129.       Top             =   210
  130.       Width           =   2235
  131.    End
  132. Attribute VB_Name = "frmSample"
  133. Attribute VB_GlobalNameSpace = False
  134. Attribute VB_Creatable = False
  135. Attribute VB_PredeclaredId = True
  136. Attribute VB_Exposed = False
  137. Option Explicit
  138. Dim ppDoc As PreviewPrint.Document
  139. Dim nPage As Integer
  140. Private Sub btnClose_Click()
  141.     Form_Unload False
  142.     End
  143. End Sub
  144. Private Sub btnDraw_Click()
  145.     Dim nCentre As Integer
  146.     'get the page center
  147.     nCentre = ppDoc.PageWidth / 2
  148.     'draw the page title in black arial 24pt bold underline,
  149.     'centred and starting 25mm down from the page top
  150.     ppDoc.SetTextColor vbBlack
  151.     ppDoc.SetFont "Arial", 240, PPF_BOLD + PPF_UNDERLINE, 0
  152.     ppDoc.SetTextAlign PPA_NOUPDATECP + PPA_CENTER + PPA_TOP
  153.     ppDoc.TextOut nCentre, 250, "PreviewPrint: demo page " & nPage
  154.     Dim nY As Integer, nSize As Integer
  155.     Dim nWidth As Integer, nHeight As Integer
  156.     nY = 520
  157.     nSize = 50
  158.     'draw text in different colours inside this boxed area
  159.     Dim nColor As Integer
  160.     For nColor = 0 To 27
  161.         ppDoc.SetTextColor RGB((nColor / 9) * 128, ((nColor / 3) Mod 3) * 128, (nColor Mod 3) * 128)
  162.         ppDoc.SetFont "Arial", nSize, 0, 0
  163.         ppDoc.TextOut nCentre, nY, "Arial, " & (nSize / 10) & " pt, color " & ppDoc.GetTextColor
  164.         ppDoc.GetTextExtent "abc", nWidth, nHeight
  165.         nY = nY + nHeight + 20
  166.         nSize = nSize + 2
  167.     Next
  168.     'draw a box surrounding the area that we will show different
  169.     'text colours, do the lines in different colors and styles
  170.     ppDoc.MoveTo 500, 500
  171.     ppDoc.SetLineStyle PPL_DASHDOTDOT, 0, vbYellow
  172.     ppDoc.LineTo ppDoc.PageWidth - 500, 500
  173.     ppDoc.SetLineStyle PPL_DASH, 0, vbRed
  174.     ppDoc.LineTo ppDoc.PageWidth - 500, nY
  175.     ppDoc.SetLineStyle PPL_DOT, 0, vbBlue
  176.     ppDoc.LineTo 500, nY
  177.     ppDoc.SetLineStyle PPL_DASHDOT, 0, vbGreen
  178.     ppDoc.LineTo 500, 500
  179.     'demonstrate a clipped text output, draw the clipping rectangle
  180.     ppDoc.SetLineStyle PPL_SOLID, 10, vbBlack
  181.     ppDoc.MoveTo 250, nY + 100
  182.     ppDoc.LineTo nCentre - 125, nY + 100
  183.     ppDoc.LineTo nCentre - 125, nY + 400
  184.     ppDoc.LineTo 250, nY + 400
  185.     ppDoc.LineTo 250, nY + 100
  186.     'output clipped text
  187.     ppDoc.SetTextColor vbBlue
  188.     ppDoc.SetFont "Arial", 1200, PPF_BOLD, 0
  189.     ppDoc.SetTextAlign PPA_NOUPDATECP + PPA_LEFT + PPA_BASELINE
  190.     ppDoc.TextOutClip 250, nY + 400, "Clipped", _
  191.         250, nY + 100, nCentre - 125, nY + 400
  192.     'demonstrate a clipped text output, draw the clipping rectangle
  193.     ppDoc.MoveTo nCentre + 125, nY + 100
  194.     ppDoc.LineTo ppDoc.PageWidth - 250, nY + 100
  195.     ppDoc.LineTo ppDoc.PageWidth - 250, nY + 400
  196.     ppDoc.LineTo nCentre + 125, nY + 400
  197.     ppDoc.LineTo nCentre + 125, nY + 100
  198.     'output clipped text
  199.     ppDoc.SetTextColor RGB(0, 128, 0)
  200.     ppDoc.SetFont "Arial", 120, PPF_BOLD, 0
  201.     ppDoc.SetTextAlign PPA_NOUPDATECP + PPA_CENTER + PPA_TOP
  202.     ppDoc.TextOutClip (3 * nCentre) / 2, nY + 150, _
  203.         "Clipped Text Example With Long Text To Be Clipped Off Each End", _
  204.         nCentre + 125, nY + 100, ppDoc.PageWidth - 250, nY + 400
  205. End Sub
  206. Private Sub btnEndDoc_Click()
  207.     'end the document
  208.     ppDoc.EndDoc
  209.     'buttons
  210.     btnNewDoc.Enabled = True
  211.     btnNewPage.Enabled = False
  212.     btnDraw.Enabled = False
  213.     btnEndDoc.Enabled = False
  214.     btnPreview.Enabled = True
  215. End Sub
  216. Private Sub btnNewDoc_Click()
  217.     'begin a new document
  218.     ppDoc.BeginDoc
  219.     ppDoc.SetTitle "Sample PreviewPrint Document", "PreviewPrintSample"
  220.     ppDoc.StartPage = 1
  221.     'buttons
  222.     btnNewDoc.Enabled = False
  223.     btnNewPage.Enabled = True
  224.     btnDraw.Enabled = True
  225.     btnEndDoc.Enabled = True
  226.     btnPreview.Enabled = False
  227.     nPage = 1
  228. End Sub
  229. Private Sub btnNewPage_Click()
  230.     'start a new page now
  231.     ppDoc.NewPage
  232.     nPage = nPage + 1
  233. End Sub
  234. Private Sub btnPreview_Click()
  235.     'hide ourselves while the preview window is shown
  236.     Me.Hide
  237.     'show the preview window and start the timer which will
  238.     'check to see when we should show ourselves again
  239.     ppDoc.ShowPreview
  240.     Timer.Enabled = True
  241. End Sub
  242. Private Sub btnSetup_Click()
  243.     'show the printer setup
  244.     ppDoc.ShowPrintSetup
  245. End Sub
  246. Private Sub Form_Load()
  247.     On Error GoTo errLoad
  248.     'ensure that we show ourselves before we load the
  249.     'previewprint object, show the hourglass while we do so
  250.     Me.Show
  251.     Screen.MousePointer = vbHourglass
  252.     DoEvents
  253.     'create the ole automation server object, we will use this
  254.     'object to draw all of our pages and never touch the VB
  255.     'printer object again
  256.     Set ppDoc = New PreviewPrint.Document
  257.     Screen.MousePointer = vbDefault
  258.     'set the initial values
  259.     ppDoc.hwndOwner = Me.hWnd
  260.     ppDoc.Orientation = PPO_PORTRAIT
  261.     Exit Sub
  262. errLoad:
  263.     Screen.MousePointer = vbDefault
  264.     MsgBox _
  265.         "Unable to create the PreviewPrint object. " _
  266.         & "Ensure it has been registered by running the program once. " _
  267.         & Err.Description
  268.     End
  269. End Sub
  270. Private Sub Form_Unload(Cancel As Integer)
  271.     'destroy the ole automation server object
  272.     ppDoc.KillDoc   'not strictly necessary
  273.     Set ppDoc = Nothing
  274. End Sub
  275. Private Sub Timer_Timer()
  276.     Dim nState As Integer
  277.     'while we are still in the preview state, do nothing
  278.     nState = ppDoc.PreviewPrintState
  279.     If nState <> PPS_PREVIEW Then
  280.         'we've stopped previewing, so now we are either printing
  281.         'or we have cancelled the print/preview and are returned
  282.         'to our main app.  Cancel the timer and make ourselves
  283.         'visible
  284.         Timer.Enabled = False
  285.         Me.Visible = True
  286.         
  287.         'if we are about to print, then we need to set our zorder
  288.         'so that we are behind the print dialog, otherwise if we
  289.         'are idle again then we want to move to the front of the zorder
  290.         'and get the focus again
  291.         If nState = PPS_PRINT Then
  292.             Me.ZOrder 1
  293.         Else
  294.             Me.ZOrder
  295.             Me.SetFocus
  296.         End If
  297.     End If
  298. End Sub
  299.